﻿using LiteDB.Async;
using Microsoft.AspNetCore.Mvc;
using WebAppDbTest.Models;
using WebAppDbTest.Services;

namespace WebAppDbTest.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class LiteDbController : ControllerBase
{
    #region 构造函数 DI
    private readonly IAppLogsServices _logger;
    private readonly IWeatherForecastServices _weatherForecastServices;

    public LiteDbController(IAppLogsServices logger,
        IWeatherForecastServices weatherForecastServices)
    {
        _logger = logger;
        _weatherForecastServices = weatherForecastServices;
    }
    #endregion

    #region About LiteDB CRUD
    /// <summary>
    /// 单条数据添加
    /// </summary>
    /// <returns></returns>
    [HttpPost(Name = "AddSingle")]
    public async Task<Guid> AddSingleAsync()
    {
        // 模拟数据
        var wf = _weatherForecastServices.GetWeatherForecasts(1).First();
        return await _weatherForecastServices.LiteDbAddSingleAsync(nameof(WeatherForecast), wf);
    }

    /// <summary>
    /// 批量数据添加
    /// </summary>
    /// <returns></returns>
    [HttpPost(Name = "AddBulk")]
    public async Task<int> AddBulkAsync(int conut)
    {
        // 模拟数据
        var wfs = _weatherForecastServices.GetWeatherForecasts(conut);
        return await _weatherForecastServices.LiteDbAddBulkAsync(nameof(WeatherForecast), wfs);
    }

    /// <summary>
    /// Id 查询单条数据
    /// </summary>
    /// <param name="id">Guid</param>
    /// <returns></returns>
    [HttpGet(Name = "GetSingle")]
    public async Task<WeatherForecast> GetSingleAsync(Guid id)
    {
        return await _weatherForecastServices.LiteDbGetSingleAsync<WeatherForecast>(nameof(WeatherForecast), id);
    }

    /// <summary>
    /// 查询所有数据
    /// </summary>
    /// <returns></returns>
    [HttpGet(Name = "GetAll")]
    public async Task<IEnumerable<Guid>> GetAllAsync()
    {
        var wfs = await _weatherForecastServices.LiteDbGetAllAsync<WeatherForecast>(nameof(WeatherForecast));

        var guids = new List<Guid>();
        foreach (var item in wfs)
        {
            guids.Add(item.Id);
        }

        return guids;
    }

    /// <summary>
    /// 更新数据
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    [HttpPut(Name = "UpdateSingle")]
    public async Task<bool> UpdateSingleAsync([FromBody] Guid id)
    {
        // 模拟数据
        var wf = _weatherForecastServices.GetWeatherForecasts(1).First();
        wf.Id = id;

        return await _weatherForecastServices.LiteDbUpdateSingleAsync(nameof(WeatherForecast), wf);
    }

    /// <summary>
    /// 批量更新
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    [HttpPut(Name = "UpdateBulk")]
    public async Task<int> UpdateBulkAsync([FromBody] List<Guid> ids)
    {
        var wfs = _weatherForecastServices.GetWeatherForecasts(ids.Count).ToList();
        for (int i = 0; i < ids.Count; i++)
        {
            wfs[i].Id = ids[i];
        }

        return await _weatherForecastServices.LiteDbUpdateBulkAsync(nameof(WeatherForecast), wfs);
    }

    /// <summary>
    /// Id 删除单条数据
    /// </summary>
    /// <param name="id">Guid</param>
    /// <returns></returns>
    [HttpDelete(Name = "DeleteSingle")]
    public async Task<bool> DeleteSingleAsync(Guid id)
    {
        return await _weatherForecastServices.LiteDbDeleteSingleAsync<WeatherForecast>(nameof(WeatherForecast), id);
    }

    /// <summary>
    /// 删除所有
    /// </summary>
    /// <returns></returns>
    [HttpDelete(Name = "DeleteBulk")]
    public async Task<int> DeleteBulkAsync(List<Guid> ids)
    {
        return await _weatherForecastServices.LiteDbDeleteBulkAsync<WeatherForecast>(nameof(WeatherForecast), x => ids.Contains(x.Id));
    }
    #endregion

}
